package com.sxkf.template.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.api.vo.R;
import com.sxkf.template.entity.ShoppingCart;
import com.sxkf.template.entity.Store;
import com.sxkf.template.service.ShoppingCartService;
import com.sxkf.template.vo.response.cart.QueryUserCartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* (t_shopping_cart)表控制层
*
* @author zgc
*/
@Api(tags="商品购物车模块")
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Resource
    private ShoppingCartService shoppingCartService;
    
    @ApiOperation("查询用户的购物车")
    @GetMapping("/listUserCart")
    public R<List<Store>> listUserCart(@RequestParam String userId){
        return R.success(shoppingCartService.listUserCart(userId));
    }
    
    @ApiOperation("删除购物车中的商品")
    @PostMapping("/deleteByUserId")
    public R<Boolean> deleteByUserId(@RequestBody String ids){
        String ids1 = JSONUtil.parseObj(ids).getStr("ids");
        List<String> idList = Arrays.asList(ids1.split(","));
        return R.success(shoppingCartService.removeBatchByIds(idList));
    }
    
    @ApiOperation("更新购物车中商品信息")
    @PostMapping("/updateById")
    public R<Boolean> updateById(@RequestBody ShoppingCart shoppingCart){
        return R.success(shoppingCartService.updateById(shoppingCart));
    }

    @ApiOperation("添加到购物车")
    @PostMapping("/add")
    public R<Boolean> add(@RequestBody ShoppingCart shoppingCart){
        shoppingCartService.add(shoppingCart);
        return R.success();
    }
}
